const router = require('koa-router')()
const { login,register, list ,rentCheck, applyRent, banUser,applyRentPass,user,changeUser} = require('../dao/usersDao')
var jwt = require('jsonwebtoken');

router.prefix('/users')

router.post('/login', async function (ctx, next) {
  const result = {}
  console.log(ctx.request.body)
  const res = await login(ctx.request.body);
  console.log(3333,res[0])
  if(res[0]){
    const {name,password,_id} = res[0];
    const token = jwt.sign({id:_id,name,exp: Date.now() / 1000 + (60 * 60 * 24)},"PRIVATE_KEY")
    result.userInfo = {name,password,_id};
    result.token= token;
  }else {
    result.msg='登陆失败'
  }
  console.log(result)
  ctx.body = result;
})

router.post('/collection', async function (ctx, next) {
  const result = {}
  console.log(ctx.request.body)
  const res = await collectionAdd(id)
  ctx.body = res;
})
router.post('/findUser', async function (ctx, next) {
  const result = {}
  console.log(ctx.request.body)
  const {id} = ctx.request.body
  console.log(123,id)
  const res = await user(id);
  console.log(res)
  ctx.body = res;
})
router.post('/changeUser', async function (ctx, next) {
  const result = {}
  const user = ctx.request.body
  const res = await changeUser(user);
  ctx.body = result;
})


router.post('/register', async function (ctx, next) {
  const result ={}
  const res = await register(ctx.request.body);
  console.log(123123,ctx.request.body)
  if(res._id){
    const {username,password,_id} = res;
    const token = jwt.sign({id:_id,username,exp: Date.now() / 1000 + (60 * 60 * 24)},"PRIVATE_KEY")
    result.userInfo = {username,password};
    result.token= token;
    result.stateCode = 200;
    result.msg='注册成功'
  }else {
    result.stateCode = 400;
    result.msg = '注册失败'
  }
  // if(res)
  //
  ctx.body= result
})

router.post('/rentCheck', async function (ctx, next) {
  const {id}= ctx.request.body
  const res = await rentCheck(id);
  console.log(res)
  if(res.length>=1){
    const {username,password,_id} = res;
    result.data = res
    result.stateCode = 200;
    result.msg='获取成功'
  }else {
    result.stateCode = 400;
    result.msg = '获取失败'
  }
  ctx.body= result
})
router.post('/ban', async function (ctx, next) {
  const result={}
  const {id,ban}= ctx.request.body
  const res = await banUser({id,ban});
  if(true){
    result.data = res
    result.stateCode = 200;
    result.msg='冻结状态更新成功'
  }else {
    result.stateCode = 400;
    result.msg = '更新失败'
  }
  ctx.body = result
})
//租车审核
router.post('/applyRent', async function (ctx, next) {
  const result={}
  const {id}= ctx.request.body
  const res = await applyRent(id);
  if(res.n===1){
    const {username,password,_id} = res;
    result.data = res
    result.stateCode = 200;
    result.msg='申请审核成功'
  }else {
    result.stateCode = 400;
    result.msg = '申请审核失败'
  }
  ctx.body = result
})
//租车审核通过
router.post('/applyRentPass', async function (ctx, next) {
  const result={}
  console.log(ctx.request.body)
  const {id,pass}= ctx.request.body

  console.log(123,id,pass)
  const res = await applyRentPass({id,pass});
  if(true){

    result.data = res
    result.stateCode = 200;
    result.msg='审核状态更新成功'
  }else {
    result.stateCode = 400;
    result.msg = '更新状态失败'
  }
  ctx.body = result
})
router.get('/list', async function (ctx, next) {
  const result = {}
  const res = await list();

  if(res.length>=1){
    result.data = res
    result.stateCode = 200;
    result.msg='获取成功'
  }else {
    result.stateCode = 400;
    result.msg = '获取失败'
  }
  ctx.body=result
})




module.exports = router
